///===================增加商品 js


// 税率自定义下拉数据源, 目前查询不支持排序,此方法用来排序
function taxRateDropdownData(evt) {


    console.log(evt);

    // 目前组织作为查询条件,查不到数据,这是个bug
    // evt.data.filter.organization_id= [{"id":"1","name":"上海固买供应链管理有限公司","type":1}];
    var compareFun = (a, b) => Number.parseFloat(a.tax_rate) - Number.parseFloat(b.tax_rate);
    var formatName = a => a.percents = Number.parseInt(Number.parseFloat(a.tax_rate) * 100) + '%';
    h3ui.models.t_goods_tax_rate.listBO(evt.data).then(res => {
        res.items.sort(compareFun).forEach(formatName);
        evt.node.props.dataSource = res;
    })

}


// 单位自定义下拉数据源
function unitDropdownData(evt) {


    console.log(evt);

    // 目前组织作为查询条件,查不到数据,这是个bug
    // evt.data.filter.organization_id= [{"id":"1","name":"上海固买供应链管理有限公司","type":1}];

    h3ui.models.t_goods_unit.listBO(evt.data).then(res => {
        evt.node.props.dataSource = res;
    })

}


//初始化

function myPageInit() {
    console.log(123123);

    var actionName = h3ui.$router.currentRoute.query.mode;

    if (actionName == "update") {
        h3ui.widgets.Label3.props.content = "编辑商品";
    } else {
        h3ui.widgets.Label3.props.content = "新增商品";
    }

    //初始化事件监听
    h3ui.models.t_goodses.subscribe('inited', function () {
        console.log(" // inited 事件");
        formRequired();
        // 清空监听：
        h3ui.models.t_deliver_notice.subscribe('inited', function () {
        });
    })


}

// 表单 必填字段控制
function formRequired() {
    console.log(" // 需要 必填的字段");
    var h3Model = h3ui.models.t_goodses;
    // 需要 必填的字段
    var requiredField = ["brand_id", "name", "model", "spec", "catalog_id", "unit_id",
        "package_num", "package_unit_id", "delivery", "delivery_place", "expiry", "tax_rate_id", "pretax_price"];

    var filterProperties = a => requiredField.includes(a.code);

    var setRequired = a => a.required = true;

    h3Model.schema.properties.filter(filterProperties).forEach(setRequired);

    h3Model.newItem();
}


// mode
// create
// detail
// clickcloumn

function saveOrUpdate(evt) {
    console.log(evt);
    //提交校验
    if (!h3ui.models.t_goodses.validate()) {
        return false;
    }

    var actionName = h3ui.$router.currentRoute.query.mode;
    var dataSource = h3ui.models.t_goodses;
    itemDefualt(dataSource.item);


    var data = JSON.parse(JSON.stringify(dataSource.item));
    dataSource.convert(data, dataSource.schema);

    var schemaCode = dataSource.schemaCode;

    var param = {data, schemaCode, actionName};
    h3ui.services.manageGoodsSave(param).then(res => {

        if (res.success) {
            if (evt.data.name == "提交") {
                h3ui.$message.success('提交成功');
                if (dataSource.item.id == null) {
                    dataSource.item.id = res.data;
                }

                setTimeout(window.history.back(), 2);
            } else {
                h3ui.$message.success('保存成功');

                if (actionName == "update") {
                    setTimeout(window.history.back(), 2);
                }

                if (dataSource.item.id == null) {
                    dataSource.item.id = res.data;
                }

            }
        } else {

            h3ui.$message.error(res.errMessage || '提交失败');
        }


    });
}

function itemDefualt(item) {
    if (!item.organization_id) {
        var orgInfo = JSON.parse(localStorage.getItem("orgInfo"));
        item.vendor_id = {id: orgInfo.deptId, name: orgInfo.name, type: 1};
        item.org_fullname = orgInfo.name;
    }
}